package cn.itcast.order.service;

import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.feign.clients.UserClient;
import cn.itcast.order.pojo.Order;
import cn.itcast.feign.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    //注入RestTemplate
//    @Autowired
//    private RestTemplate restTemplate;
    //注入Feign客户端
    @Autowired
    private UserClient userClient;

    public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
//        //2.通过用户id查询用户信息
//        String url = "http://userservice/user/"+order.getUserId();
//        //利用restTemplate对象调用getobject方法得到user对象信息
//        User user = restTemplate.getForObject(url, User.class);

        //2.现在使用Feign客户端实现远程调用查询用户信息服务
        User user = userClient.findById(order.getUserId());
        //3.将用户信息存到订单中
        order.setUser(user);
        // 4.返回
        return order;
    }
}
